/* eslint-disable @typescript-eslint/no-unused-vars */
//	公开分享设置modal

import React, { useEffect, useState } from 'react';
import { Modal, Button, message, Tree, Card } from 'antd';
import { getResourcePermission, getShareUrl, selectOrgPosWithEmpTree } from 'src/services/AnalyseManage';
import { mapTreeData } from 'src/libs/utils';
import styles from '../index.module.scss';

interface IProps {
	shareModalVisible: boolean;
	handleOk: ( type, password ) => void;
	handleCancel: () => void;
	pageId: string;
	isPublic: string;
}

const ShareModal = ( props: IProps ) => {
	const [ shareLink, setShareLink ] = useState( '' );
	const [ treeData, setTreeData ] = useState( [] );
	const [ viewUserList, setViewUserList ] = useState( [] );
	const [ editUserList, setEditUserList ] = useState( [] );

	const handleShareUrl = async () => {
		const data = {
			pageId: props.pageId
		};
		const res = await getShareUrl( data );
		if ( res.data.data ) {
			setShareLink( `${res.data.data.address}/${res.data.data.code}` );
		}
	};

	const getTreeData = async () => {
		const data = {
			type: 'ORGANIZATION'
		};
		const res = await selectOrgPosWithEmpTree( data );
		if ( res.data.data ) {
			let treeData = res.data.data.map( org => mapTreeData( org ) );
			setTreeData( treeData );
			// console.log( treeData );
		}
	};

	const getPermission = async () => {
		const data = {
			id: props.pageId,
			resourceType: 'page'
		};
		const res = await getResourcePermission( data );
		if ( res.data ) {
			setViewUserList( res.data.data.viewUserList );
			setEditUserList( res.data.data.editUserList );
			// setCheckTreeData()
			// console.log( res.data.data.viewUserList, res.data.data.editUserList );
		}
	};

	const handleCopy = () => {
		const input = document.createElement( 'input' );
		input.setAttribute( 'value', shareLink );
		document.body.appendChild( input );
		input.select();
		if ( document.execCommand( 'copy' ) ) {
			document.execCommand( 'copy' );
			message.success( '复制成功' );
		} else {
			message.warn( '您的浏览器暂不支持该功能' );
		}
		document.body.removeChild( input );
	};

	useEffect( () => {
		console.log( props );
		if ( props.isPublic === '1' ) {
			handleShareUrl();
		} else if ( props.isPublic === '0' ) {
			getTreeData();
			getPermission();
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [ props.isPublic ] );

	return (
		<Modal
			title={props.isPublic === '0' ? '非公开报表':'公开报表'}
			className={styles.share_modal}
			visible={props.shareModalVisible}
			width="800px"
			footer={
				[] // 设置footer为空，去掉 取消 确定默认按钮
			}
			onCancel={props.handleCancel}
		>
			{props.isPublic === '1' && ( <div>
				<a href={shareLink} rel="noopener noreferrer" target='_blank'>{shareLink}</a>
				<div>
					<Button type='primary' ghost style={{ marginTop: '20px' }} onClick={handleCopy}>复制链接</Button>
				</div>
			</div> )}
			{props.isPublic === '0' && ( <div className={styles.auth_container}>
				<Card title="可见权限" style={{ width: '100%' }}>
					<Tree
						disabled={true}
						checkable
						checkedKeys={viewUserList}
						treeData={treeData}
					/>
				</Card>
				<Card title="编辑权限" style={{ width: '100%' }}>
					<Tree
						disabled={true}
						checkable
						checkedKeys={editUserList}
						treeData={treeData}
					/>
				</Card>
			</div> )}
		</Modal>
	);
};

export default React.memo( ShareModal );
